

#include "mainwidget.h"
#include "ui_mainwidget.h"
#include "screenshotwindow.h"
#include "showpicture.h"
#include "shortcutsetting.h"
#include "cfg.h"

#include <QActionEvent>
#include <QShortcut>
#include "QHotkey"
#include <QMessageBox>

#include <QDebug>

MainWidget::MainWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
    , m_SystemTray(this)
    , m_TrayMenu(this)
    , m_trayExitAction(nullptr)
    , m_trayShowAction(nullptr)
    , m_trayScreenshotAction(nullptr)
    , m_Hotkey(this)
    , m_bCapture(false)
{
    ui->setupUi(this);
    ui->screenshotPB->setIcon(QIcon("://image/icon_svg"));
    connect(ui->screenshotPB, &CCustomToolButton::clicked, this, &MainWidget::startScreenShot);
    createSystemTray();
    QString qsKeySeq = Cfg::ReadScreenshotShortcut();
    if (qsKeySeq.isEmpty())
    {
        m_Hotkey.setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_F1));
    }
    else
    {
        m_Hotkey.setShortcut(QKeySequence::fromString(qsKeySeq));
    }
    connect(&m_Hotkey, &QHotkey::activated, this, &MainWidget::startScreenShot);
    connect(&m_Hotkey, &QHotkey::registeredChanged, this, &MainWidget::onScreenShortcutRegisterChanged);
    m_Hotkey.setRegistered(true);
    if (!m_Hotkey.isRegistered())
    {
        QMessageBox::warning(this, "pixel", tr("热键注册失败:")+m_Hotkey.shortcut().toString());
    }
}

MainWidget::~MainWidget()
{
    delete ui;
}

void MainWidget::startScreenShot()
{
    if (!m_bCapture)
    {
        m_bCapture = true;
        ScreenshotWindow screenWindow;
        connect(&screenWindow, QOverload<const QPixmap&, const QRect&>::of(&ScreenshotWindow::pasteOnTheScreen), \
                this, &MainWidget::onPasteOnScreen);
        screenWindow.exec();
        m_bCapture = false;
    }
}

void MainWidget::onSystemTrayActived(QSystemTrayIcon::ActivationReason reason)
{
    switch (reason)
    {
    case QSystemTrayIcon::Context:
        showMenu();
        break;
    case QSystemTrayIcon::Trigger:
        showNormal();
        break;
    default:
        break;
    }
}

void MainWidget::onShowWindowActionSlot()
{
    if (isMinimized())
    {
        setWindowState(Qt::WindowActive);
        showNormal();
    }
}

void MainWidget::onExitActionSlot()
{
    close();
}

void MainWidget::onPasteOnScreen(const QPixmap &pixmap, const QRect& rect)
{
    CShowPicture *pPic = new CShowPicture(pixmap);
    if (pPic)
    {
        pPic->move(rect.topLeft());
        pPic->show();
    }
}

void MainWidget::onScreenShortcutRegisterChanged(bool b)
{
    if (b)
    {
        m_trayScreenshotAction->setText(tr("截屏"));
        m_trayScreenshotAction->setShortcut(m_Hotkey.shortcut());
    }
    else
    {
        m_trayScreenshotAction->setText(tr("截屏(未注册快捷)"));
        m_trayScreenshotAction->setShortcut(QKeySequence());
    }
}

void MainWidget::onShortcutSet()
{
    CShortcutSetting shortcutSet(this);
    shortcutSet.setAttribute(Qt::WA_QuitOnClose, false);
    shortcutSet.SetScreenshotShortcut(m_Hotkey.shortcut());
    shortcutSet.exec();
    QKeySequence newShortcut = shortcutSet.ScreenShortcut();
    if (m_Hotkey.shortcut().matches(newShortcut) != QKeySequence::ExactMatch)
    {
        //热键有变化，重新注册
        if (m_Hotkey.isRegistered())
        {
            m_Hotkey.setRegistered(false);
        }
        m_Hotkey.setShortcut(newShortcut);
        QString qsShortcut = m_Hotkey.shortcut().toString();
        if (!m_Hotkey.setRegistered(true))
        {
            m_trayScreenshotAction->setText(tr("截屏(未注册快捷)"));
            m_trayScreenshotAction->setShortcut(QKeySequence());
            QMessageBox::warning(this, "pixel", tr("热键注册失败:")+qsShortcut);
        }
        Cfg::SaveScreenshotShortcut(qsShortcut);
    }
}

void MainWidget::keyPressEvent(QKeyEvent *event)
{
    Q_UNUSED(event)
}

void MainWidget::createSystemTray()
{
    m_SystemTray.setToolTip(tr("pixel"));
    m_SystemTray.setIcon(QIcon("://image/icon_svg"));
    m_trayScreenshotAction = m_TrayMenu.addAction(tr("截屏"), this, &MainWidget::startScreenShot);
    m_TrayMenu.addAction(tr("修改快捷键"), this, &MainWidget::onShortcutSet);
//    m_trayShowAction = m_pTrayMenu->addAction(tr("显示主界面"), this, SLOT(OnShowWindowActionSlot()));
    m_trayExitAction = m_TrayMenu.addAction(tr("退出"), this, &MainWidget::onExitActionSlot);
    m_SystemTray.setContextMenu(&m_TrayMenu);
    m_SystemTray.show();
}

void MainWidget::showMenu()
{
    m_TrayMenu.show();
}

void MainWidget::KeyCapture(int key)
{
    if (Qt::Key_F1 == key)
    {
        startScreenShot();
    }
}

